package controllers

import (
	"encoding/json"
	"gitee.com/lzqysoft/ims/libs"
	"gitee.com/lzqysoft/ims/libs/hugo"
	"gitee.com/lzqysoft/ims/models"
	"os"
	"time"

	"github.com/astaxie/beego"
)

type ArticleController struct {
	CommonController
}
type articleJson struct {
	Id      string
	Name    string
	Banner  string
	Source  string
	Top     int
	Menu    string
	Status  int
	WeiChat int
	Summary string
	Html    string
	Author  string
	Content string
	Tags    string
}

func (c *ArticleController) URLMapping() {
	c.Mapping("Post", c.Post)
	c.Mapping("GetArticleByMenuId", c.GetArticleByMenuId)
	c.Mapping("Update", c.Update)
	c.Mapping("Delete", c.Delete)
}

// @Title Post
// @Description create Article
// @Param	body		body 	models.Menu	true		"body for Menu content"
// @Success 201 {int} models.Article
// @Failure 403 body is empty
// @router / [post]
func (c *ArticleController) Post() {
	user := c.User
	var v models.Article
	rdata, err := c.AesDecodeToByte(c.GetString("tdata"))
	if err != nil {
		beego.Debug(err)
		c.RetError(ErrToken)
	}
	var m articleJson
	if err = json.Unmarshal(rdata, &m); err != nil {
		beego.Debug(err)
		c.RetError(ErrInputData)
	}
	v.Id = libs.UUID()
	v.Name = m.Name
	v.Seq = 0
	v.Name = m.Name
	v.Status = m.Status
	v.Remark = ""
	v.Banner = m.Banner
	v.Top = m.Top
	v.Source = m.Source
	v.Author = m.Author
	v.Content = m.Content
	v.Summary = m.Summary
	v.Tags = m.Tags
	menu, err := models.GetMenuById(m.Menu)
	if err != nil {
		c.RetError(ErrInputData)
	}
	v.Menu = menu
	//v.Status, _ = strconv.Atoi(sdata["Status"].(string))
	v.Creater = user.Name
	v.Ctime = time.Now()
	v.Updater = v.Creater
	v.Utime = v.Ctime
	v.Status = 1
	v.IsDelete = 0
	path := beego.AppConfig.String("WebRootDir") + "/" + user.Organization.Context + "/content/" + menu.Folder + "/"
	locate, err := hugo.Writemd(v.Name, user.Abbr, v.Banner, path, v.Content, []string{"ims"})
	if err != nil {
		beego.Debug(err)
		c.RetError(ErrInputData)
	}
	v.Locate = locate
	if _, err = libs.Hugo(beego.AppConfig.String("WebRootDir") + "/" + user.Organization.Context); err != nil {
		beego.Debug(err)
		c.RetError(ErrFds)
	}
	if err := models.AddArticle(&v); err != nil && err.Error() != "no LastInsertId available" {
		beego.Debug(err)
		c.RetError(ErrDatabase)
	}

	c.Dlog("添加了新闻，id是：", v.Id)
	c.RetSuccess(v.Id)
}

// @Title Get
// @Description get Articles by menu
// @Param	body		body 	models.	true		"body for Menu content"
// @Success 201 {int} models.Article
// @Failure 403 body is empty
// @router /GetArticleByMenuId [post]
func (c *ArticleController) GetArticleByMenuId() {
	//user := c.User
	rdata, err := c.AesDecode(c.GetString("tdata"))
	if err != nil {
		c.RetError(ErrToken)
	}
	sdata, _ := rdata.(map[string]interface{})
	id := sdata["Id"].(string)
	article, err := models.GetArticleByMenu(id)
	if err != nil {
		beego.Debug(err)
		c.RetError(ErrInputData)
	}
	c.RetSuccess(article)
}

// @Title Update
// @Description Update Articles
// @Param	body		body 	models.Article	true		"body for Menu content"
// @Success 201 {int} models.Article
// @Failure 403 body is empty
// @router /Update [post]
func (c *ArticleController) Update() {
	user := c.User
	rdata, err := c.AesDecodeToByte(c.GetString("tdata"))
	if err != nil {
		beego.Debug(err)
		c.RetError(ErrToken)
	}
	var m articleJson
	if err = json.Unmarshal(rdata, &m); err != nil {
		beego.Debug(err)
		c.RetError(ErrInputData)
	}
	v, err := models.GetArticleById(m.Id)
	if err != nil {
		c.RetError(ErrInputData)
	}
	v.Name = m.Name
	v.Source = m.Source
	v.Banner = m.Banner
	v.Updater = user.Abbr
	v.Utime = time.Now()
	v.Top = m.Top
	v.Status = m.Status
	v.Remark = ""
	v.Author = m.Author
	v.Content = m.Content
	v.Summary = m.Summary
	v.Tags = m.Tags
	beego.Debug(v.Locate)
	if err = os.Remove(v.Locate); err != nil {
		beego.Debug(err)
		c.RetError(ErrFds)
	}
	menu, err := models.GetMenuById(m.Menu)
	if err != nil {
		c.RetError(ErrInputData)
	}
	path := beego.AppConfig.String("WebRootDir") + "/" + user.Organization.Context + "/content/" + menu.Folder + "/"
	locate, err := hugo.Writemd(v.Name, user.Abbr, v.Banner, path, v.Source, []string{"ims"})
	if err != nil {
		beego.Debug(err)
		c.RetError(ErrInputData)
	}
	v.Locate = locate
	if _, err = libs.Hugo(beego.AppConfig.String("WebRootDir") + "/" + user.Organization.Context); err != nil {
		beego.Debug(err)
		c.RetError(ErrFds)
	}
	if err := models.UpdateArticle(v); err != nil && err.Error() != "no LastInsertId available" {
		beego.Debug(err)
		c.RetError(ErrDatabase)
	}
	c.Dlog("修改了新闻，id是：", v.Id)
	c.RetSuccess(v.Id)
}

// @Title Delete
// @Description Delete Articles
// @Param	body		body 	models.Article	true		"body for Menu content"
// @Success 201 {int} models.Article
// @Failure 403 body is empty
// @router /Delete [post]
func (c *ArticleController) Delete() {
	user := c.User
	//rdata, err := c.AesDecodeToByte(c.GetString("tdata"))
	//if err != nil {
	//	beego.Debug(err)
	//	c.RetError(ErrToken)
	//}
	//var m articleJson
	//if err = json.Unmarshal(rdata, &m); err != nil {
	//	beego.Debug(err)
	//	c.RetError(ErrInputData)
	//}
	adata, err := c.AesDecode(c.GetString("tdata"))
	if err != nil {
		beego.Debug(err)
		c.RetError(ErrToken)
	}
	fdata := adata.(map[string]interface{})
	v, err := models.GetArticleById(fdata["Id"].(string))
	if err != nil {
		beego.Debug(err)
		c.RetError(ErrInputData)
	}
	if err != os.Remove(v.Locate) {
		beego.Debug(err)
		c.RetError(ErrFds)
	}
	if err = models.DeleteArticle(v.Id); err != nil {
		beego.Debug(err)
		c.RetError(ErrFds)
	}
	if _, err = libs.Hugo(beego.AppConfig.String("WebRootDir") + "/" + user.Organization.Context); err != nil {
		beego.Debug(err)
		c.RetError(ErrFds)
	}
	c.Dlog("删除了新闻,id是:", fdata["Id"].(string))
	c.RetSuccess("delete success")

}
